package com.op.teachingliteapp.sender.ui.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.op.teachingliteapp.R;
import com.op.teachingliteapp.fragment.PPTShowFragment;
import com.op.teachingliteapp.sender.listener.onDrawListener;

public class DrawingWithBezier extends View {

	private float mX;
	private float mY;
	private static Paint mGesturePaint; // 画笔
	private static Path mPath = new Path(); // 路径
	private static int colorMUN= R.id.red;
	private static int modeMUN=13;//画笔的大小
    private Bitmap bitmap;
    

    public void setOnDrawListener(onDrawListener onDrawListener) {
        this.onDrawListener = onDrawListener;
    }

    private onDrawListener onDrawListener;


    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;

    }

    public DrawingWithBezier(Context context) {
		this(context, null);
	}

	public DrawingWithBezier(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public DrawingWithBezier(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// 实例化画笔，设置画笔的属性
		mGesturePaint = createPaint(13,  Color.rgb(255, 19, 19));
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

        // TODO Auto-generated method stub

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath = new Path();
                colorSelection(modeMUN,colorMUN);//大小，颜色
               // setsetmodeMUN(modeMUN,colorSelection(modeMUN));
                touchDown(event);
                break;
            case MotionEvent.ACTION_MOVE:
                touchMove(event);
                pathList.add(mPath);
                paintList.add(mGesturePaint);
                break;
            case MotionEvent.ACTION_UP:
                pathList.add(mPath);
                paintList.add(mGesturePaint);
                onDrawListener.drawEnd(null);
                break;
        }
        // 更新绘制
        invalidate();
        return true;
    }

    private Paint createPaint(int width, int color) {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(width);
		paint.setColor(color);
		return paint;
	}
	
	ArrayList<Paint> paintList = new ArrayList<Paint>();
	ArrayList<Path> pathList = new ArrayList<Path>();
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

        RectF rectF = new RectF(0, 0, getWidth(), getHeight());
        if (bitmap!=null) {
            canvas.drawBitmap(bitmap,null,rectF,null);
        }
        // 通过画布绘制多点形成的图形
        if (!PPTShowFragment.isScroll) {
            for (int i = 0; i < pathList.size(); i++) {
                canvas.drawPath(pathList.get(i), paintList.get(i));
            }
//            canvas.drawPath(mPath, mGesturePaint);
        }

	}

	// 手指点下屏幕时调用
	private void touchDown(MotionEvent event) {

//		 mPath.rewind();
		// 重置绘制路线，即隐藏之前绘制的轨迹
//		 mPath.reset(); //清除之前画的
		float x = event.getX();
		float y = event.getY();

		mX = x;
		mY = y;
		// mPath绘制的绘制起点
		mPath.moveTo(x, y);
	}

	// 手指在屏幕上滑动时调用
	private void touchMove(MotionEvent event) {
		final float x = event.getX();
		final float y = event.getY();

		final float previousX = mX;
		final float previousY = mY;

		final float dx = Math.abs(x - previousX); // 取绝对值
		final float dy = Math.abs(y - previousY);

		// 两点之间的距离大于等于3时，生成贝塞尔绘制曲线
		if (dx >= 3 || dy >= 3) {
			// 设置贝塞尔曲线的操作点为起点和终点的一半
			float cX = (x + previousX) / 2;
			float cY = (y + previousY) / 2;

			// 二次贝塞尔，实现平滑曲线；previousX, previousY为操作点，cX, cY为终点
			mPath.quadTo(previousX, previousY, cX, cY);

			// 第二次执行时，第一次结束调用的坐标值将作为第二次调用的初始坐标值
			mX = x;
			mY = y;
		}
	}

	public static void setmGesturePaintColor(int color) {
		colorMUN = color;
	}
	public static void setmodeMUN(int mode) {
		modeMUN = mode;//画笔大小
		Log.i("画笔大小", String.valueOf(modeMUN));
	}
	//先保留，有点逻辑问题
	private void setsetmodeMUN(int mode,int color){
		mGesturePaint = createPaint(mode, color);
	}
	
	private void colorSelection(int mode,int color) {
		
		Log.i("画笔大小2", String.valueOf(mode));
		switch (color) {
		case R.id.green:
			mGesturePaint = createPaint(mode,Color.rgb(81,207, 45));
			break;
		case R.id.red:
			mGesturePaint = createPaint(mode, Color.rgb(255, 19, 19));
			break;
		case R.id.blue:
			mGesturePaint = createPaint(mode, Color.rgb(48, 143, 161));
			break;
		case R.id.black:
			mGesturePaint = createPaint(mode, Color.rgb(37, 37, 37));
			break;
        default:
		}
		
	}
  public  void  reset() {
      paintList.clear();
      pathList.clear();

      Log.i("画笔是否为空？", String.valueOf( paintList.size()));
      Log.i("路径是否为空？", String.valueOf( pathList.size()));
  }

}
